<template>
  <div class="upload-container">
    <el-button
      :style="{ background: color, borderColor: color }"
      icon="el-icon-upload"
      size="mini"
      type="primary"
      @click="dialogVisible = true"
    >文件上传</el-button>
    <el-dialog :visible.sync="dialogVisible">
      <!-- <form ref="form">
        <div class="file_box">
          <i class="el-icon-plus" />
          <input
            ref="form_input"
            type="file"
            class="header_img"
            accept="image/png,image/gif,image/jpeg"
            multiple
          />
        </div>
        <img
          src="/assets/images/ppt.jpg"
          alt=""
          style="border: 1px solid red; width: 100px; height: 100px; display: block;"
        />
        <el-button size="mini" type="primary" @click="update">提交</el-button>
      </form>-->
      <!-- :multiple="true" -->
      <el-upload
        :file-list="fileLists"
        :show-file-list="true"
        class="editor-slide-upload"
        list-type="picture-card"
        :on-remove="handleRemove"
        :action="action"
        :header="uploadHeader"
        :data="uploadForm"
        :on-success="uploadSuccess"
        :before-upload="beforeUpload"
      >
        <el-button size="small" type="primary">上传</el-button>
      </el-upload>
      <el-button @click="dialogVisible = false">取消</el-button>
      <el-button type="primary" style="margin-bottom: 15px;" @click="submit">提交</el-button>
    </el-dialog>
  </div>
</template>

<script>
// import { getToken } from 'api/qiniu'
import { getToken } from '@/utils/auth';
import { preUpload } from '@/api/uploads';
export default {
  name: 'EditorSlideUpload',
  props: {
    color: {
      type: String,
      default: '#1890ff'
    }
  },
  data () {
    return {
      action: 'https://yes910-pub.obs.cn-south-1.myhuaweicloud.com',
      uploadHeader: {
        'Content-Type': 'multipart/form-data'
      },
      uploadForm: {},
      dialogVisible: false,
      fileLists: [], // 页面展示
      fileList: []
    };
  },
  created () { },
  methods: {
    handleRemove (file) {
      const uid = file.uid;
      const objKeyArr = Object.keys(this.fileList);
      for (let i = 0, len = objKeyArr.length; i < len; i++) {
        if (this.fileList[objKeyArr[i]].uid === uid) {
          delete this.fileList[objKeyArr[i]];
          return;
        }
      }
    },
    uploadSuccess (res, file) {
      console.log(file);
      console.log(res);
      var imageUrl = URL.createObjectURL(file.raw);
      console.log(imageUrl, res, file);
      var obj = {
        url: imageUrl
      };
      // this.fileLists.push(obj);
      this.fileLists = this.fileList;
    },
    submit () {
      this.$emit('successCBK', this.fileList);
      this.dialogVisible = false;
    },
    beforeUpload (file) {
      const imgStr = /\.(jpg|jpeg|png|JPG|PNG|JPEG)$/;
      if (!imgStr.test(file.name)) {
        this.$message.error('上传格式不对，请重新上传!');
        return false;
      }
      let continueUpload = false;
      return new Promise((resolve, reject) => {
        preUpload({
          biz: 'OPEN',
          name: file.name,
          size: file.size
        }).then(res => {
          this.action = res.data.action;
          this.uploadForm = res.data.parameters;
          var obj = {
            name: res.data.uri,
            url: res.data.uri
          };
          this.fileList.push(obj);

          console.log('this.fileList:', this.fileList);
          continueUpload = true;
          resolve(continueUpload);
        }).catch(err => {
          reject(continueUpload);
        });
      });
    }

  }
};
</script>

<style lang="scss" scoped>
.editor-slide-upload {
  margin-bottom: 20px;
  ::v-deep .el-upload--picture-card {
    width: 100%;
  }
}

.file_box {
  width: 180px;
  height: 180px;
  display: flex;
  justify-content: center;
  border: 1px dashed rgb(217, 217, 217);
  align-items: center;
  font-size: 26px;
  color: rgb(140, 147, 157);
  position: relative;
  .header_img {
    width: 100%;
    height: 100%;
    border: 1px solid blue;
    display: block;
    position: absolute;
    top: 0;
    left: 0;
    opacity: 0;
    cursor: pointer;
  }
}
</style>
